package com.yequan.thread.join;

/**
 * @author yq
 * @date 2020/09/03
 */
public class JoinDemo extends Thread {

    private Thread preThread;
    private int num;

    public JoinDemo(Thread preThread, int num) {
        this.preThread = preThread;
        this.num = num;
    }

    @Override
    public void run() {
        try {
            preThread.join();
            System.out.println(num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Thread preThread = Thread.currentThread();
        for (int i = 0; i < 5; i++) {
            JoinDemo joinDemo = new JoinDemo(preThread, i);
            joinDemo.start();
            preThread = joinDemo;
        }
    }

}
